//生成菜单
var menuItem = Vue
    .extend({
        name : 'menu-item',
        props : {
            item : {}
        },
        template : [
            '<li class="font_size_14">',
            '<a class="aaa" target="main" href="javascript:void(0);" :data_url="item.url">' +
            '<span>{{item.name}}</span><br/>' +
            '<span class="nav-English" style="">{{item.e_name}}</span>' +
            '<div v-if="item.url == \'none\'" id="switch" class="switch switch_on" onclick="setOpenShopApi()">' +
            '<div class="switch_tag switch_tag_on" id="switch-tag"></div>' +
            '</div></a>',
            '</li>'
        ].join('')
    });
// 注册菜单组件
Vue.component('menuItem', menuItem);
var vm = new Vue({
    el:"#homeVue",
    data:{
        menuList:[
            {
                "icon":"fa fa-folder",
                "name":"首页",
                "e_name":"home page",
                "url":"indexHome",
            },
            {
                "icon":"fa fa-folder",
                "name":"商品管理",
                "e_name":"product management",
                "url":"supply.html"
            },
            {
                "icon":"fa fa-folder",
                "name":"交易统计",
                "e_name":"transaction statistics",
                "url":"transaction"
            },
            {
                "icon":"fa fa-folder",
                "name":"我的订单",
                "e_name":"my order",
                "url":"myOrder"
            },
            {
                "icon":"fa fa-folder",
                "name":"我的",
                "e_name":"my",
                "url":"notification"
            },
            {
                "icon":"fa fa-folder",
                "name":"开关店铺",
                "e_name":"store's opening and closing times",
                "url":"none"
            }
        ],
        navTitleArray:[
            {navTitle:"首页",e_navTitle:"home page",titleImg:1}
        ],
        navTitle:"首页",
        e_navTitle:"home page",
        main:"indexHome",
        is_open:'',
        check_status:'',
        name:'',
        loading: true,
        url:oss_url
    },
    methods:{
        suppliesInit:function(){
            this.loading = true;
            var suppliesId = localStorage.getItem("suppliesId");
            var dataPost ={
                supplier_id:suppliesId
            };
            this.$http.post('/supplies/Indexhome/supplierInfo', dataPost, {
                emulateJSON: true
            }).then(function (res) {
                // console.log(res.data.supplier);
                this.name = res.data.supplier.name;
                this.star = res.data.supplier.star;
                if(res.data.supplier.image === ''||res.data.supplier.image === 'null'||res.data.supplier.image == null||res.data.supplier.image === undefined){
                    this.image = 'shop/default.png';
                }else{
                    this.image = res.data.supplier.image;
                }

                var status = res.data.supplier.check_status;
                this.check_status = res.data.supplier.check_status;
                this.is_open = res.data.supplier.is_open;
                if (2 !==status){
                    setSwitchShop(0);
                }else {
                    setSwitchShop(this.is_open);
                }
                if(status == 1){
                    this.main = "certification";
                    for (var i = 0 ; i<this.menuList.length-1 ;i++){
                        this.menuList[i].url = "certification";
                    }
                }else if(status == 2){
                    this.main = "indexHome";
                }else if(status ==3){
                    this.main = "failCertification";
                    for (var i = 0 ; i<this.menuList.length-1 ;i++){
                        this.menuList[i].url = "failCertification";
                    }
                }else if(status == 0){
                    var id = localStorage.getItem("suppliesId");
                    top.location.href = "/supplies/index/authenticationInfo?id="+id;
                    for (var i = 0 ; i<this.menuList.length-1 ;i++){
                        this.menuList[i].url = "certification";
                    }
                }
                this.loading = false;
               /* var _this = this;
                setTimeout(function () {
                    _this.loading = false;
                }, 300);*/
            }, function (res) {
            });
        },
        score: function (num) {
            var num1    = Math.ceil(num);      //取整数(实心)
            var num2    = 5 - num1;  //取整数(空心)
            var html = '';
            for(var i=1;i<=num1;i++){
                html += '<div class="score_fill_img"></div>';
            }
            for(var i=1;i<=num2;i++){
                html += '<div class="score_img"></div>';
            }
            return html;
        },
        // 退出登录
        logout:function () {
            localStorage.setItem("suppliesId",'');
            var url = '/apishop/Apishop/logout';
            var data = {
                // supplies_id:suppliesId
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                console.log(res.status);
                if (res.data.status == 200) {
                    window.location.href = "/supplies/index/login";
                } else {
                }
            }, function (res) {
            
            });
        }
    },
     mounted : function () {
         var suppliesId = localStorage.getItem("suppliesId")
         if (suppliesId == ''|| suppliesId == undefined){
             window.location.href = '/supplies/index/login';
             return;
         }
         this.suppliesInit();
     }
});

$(function(){
    $("#sidebar-menu li  a").on("click",function(){
        var url = $(this).attr("data_url");
        if(url == "none" || vm.menuList[0].url != "indexHome"){
            return;
        }
        vm.main = url;
        var text = $(this).find("span").eq(0).text();
        var text1 = $(this).find("span").eq(1).text();
        if(text.indexOf("首页")>=0){

        }else if(text.indexOf("商品管理")>=0){
                vm.navTitleArray[0] = {navTitle:"商品管理",e_navTitle:"product management",titleImg:1};
                vm.navTitleArray[1]= {navTitle:"商品列表",e_navTitle:"product list",titleImg:0};
                vm.navTitleArray[2] = {navTitle:"",e_navTitle:" ",titleImg:0};
        }else if(text.indexOf("交易统计")>=0){
                vm.navTitleArray[0] = {navTitle:"交易统计",e_navTitle:"transaction statistics",titleImg:1};
                vm.navTitleArray[1]= {navTitle:"买家人数",e_navTitle:"number of buyers",titleImg:0};
            vm.navTitleArray[2] = {navTitle:"",e_navTitle:" ",titleImg:0};
        }else if(text.indexOf("我的订单")>=0){
                vm.navTitleArray[0] = {navTitle: "我的订单", e_navTitle: "my order", titleImg: 1};
                vm.navTitleArray[1] = {navTitle: "订单列表", e_navTitle: "order list", titleImg: 0};
            vm.navTitleArray[2] = {navTitle:"",e_navTitle:" ",titleImg:0};
        }else if(text.indexOf("我的")>=0){
                vm.navTitleArray[0] = {navTitle: "我的", e_navTitle: "my", titleImg: 1};
                vm.navTitleArray[1] = {navTitle: "系统通知", e_navTitle: "system notification", titleImg: 0};
            vm.navTitleArray[2] = {navTitle:"",e_navTitle:" ",titleImg:0};
        }
        vm.navTitle = text;
        vm.e_navTitle = text1;
        $("#sidebar-menu li").removeClass("active");
        $(this).parent("li").addClass("active");
    })
    $("#sidebar-menu li").eq(0).addClass("active");
})
function areaEdit(new_url){
    vm.navTitleArray[0] = {navTitle:"首页",e_navTitle:"home page",titleImg:1};
    vm.navTitleArray[1] = {navTitle:"店铺管理",e_navTitle:"store management",titleImg:1};
    vm.navTitleArray[2] = {navTitle:"配送地区",e_navTitle:"distribution area",titleImg:0};
    vm.navTitle = "店铺管理";
    vm.e_navTitle="store management";
    vm.main = "/"+ new_url +"/editInfo.html?type=2";
}
function basic(new_url){
    vm.navTitleArray[0] = {navTitle:"首页",e_navTitle:"home page",titleImg:1};
    vm.navTitleArray[1] = {navTitle:"店铺管理",e_navTitle:"store management",titleImg:1};
    vm.navTitleArray[2] = {navTitle:"基本信息",e_navTitle:"baisc information",titleImg:0};
    vm.navTitle = "店铺管理";
    vm.e_navTitle="store management";
    vm.main = "/"+ new_url +"/editInfo.html?type=1";
}
function editTitle(title){
    for(var i = 0 ;i<vm.navTitleArray.length;i++){
        vm.navTitleArray.splice(i);
    }
    for(var j = 0 ;j<title.length;j++){
        vm.navTitleArray.push(title[j]);
    }
}
//设置开关状态
function  setSwitchShop(switchFlag) {
    if(0 == switchFlag){
        $("#switch").removeClass("switch_on");
        $("#switch").addClass("switch_off");
        $("#switch-tag").removeClass("switch_tag_on");
        $("#switch-tag").addClass("switch_tag_off");
    }else{
        $("#switch").removeClass("switch_off");
        $("#switch").addClass("switch_on");
        $("#switch-tag").removeClass("switch_tag_off");
        $("#switch-tag").addClass("switch_tag_on");
    }

}
//调用开关接口
function setOpenShopApi(){
    layer.confirm('确认修改店铺开关状态？<br> ' +
        'confirm that you wish to alter the opening and closing times of your store?', {
        btn: ['<span class="layer_btn_save">确定(determine)</span>', '取消(cancel)'],
        move: false
    }, function () {
        var id = localStorage.getItem("suppliesId");
        var num = vm.is_open;
        var check_status = vm.check_status;
        if (2 !==check_status){
            layer.msg('开关店铺，请在认证审核通过后',{
                skin:'winning-class',//自定义样式winning-class
                time:3000
            });
            return;
        }
        var bool = 1-num;
        $.ajax({
            type: "post",
            url: "/apishop/apishop/setOpen",
            cache: false,
            data: {id: id, is: bool},
            dataType: "json",
            success: function (data) {

                if (200 == data['status']) {
                    //关闭弹出框
                    layer.closeAll();
                    vm.is_open = bool;
                    setSwitchShop(bool);
                }
            }
        });
    });

}